<template>
    <el-container>
        <!-- 头部 -->
        <el-header>
            <div>
                <router-link to="/home">
                    <img
                        src="../assets/shop.png"
                        alt
                    />
                </router-link>
                <span>电商后台管理系统</span>
            </div>
            <div class="user">

                <div class="avatar">
                    <img
                        src="../assets/logo.png"
                        alt
                    />
                </div>
                <el-dropdown trigger="click">

                    <span class="el-dropdown-link">
                        admin
                        <i class="el-icon-arrow-down el-icon--right"></i>
                    </span>
                    <el-dropdown-menu slot="dropdown">
                        <a
                            href="#"
                            @click.prevent="logout"
                        >
                            <el-dropdown-item icon="el-icon-switch-button">退出登录</el-dropdown-item>
                        </a>
                    </el-dropdown-menu>
                </el-dropdown>
            </div>
        </el-header>
        <!-- 页面主体 -->
        <el-container>
            <!-- 左侧边栏 -->
            <el-aside :width="isCollapse ? '64px' : '200px'">
                <!-- 切换菜单的折叠和展开 -->
                <div
                    class="toggle-buttom"
                    @click="isCollapse=!isCollapse"
                >|||</div>
                <el-menu
                    active-text-color="#409eff"
                    unique-opened
                    :collapse="isCollapse"
                    :collapse-transition="false"
                    router
                    :default-active="actionPath"
                >
                    <!-- 一级菜单 -->
                    <el-submenu
                        :index="item.id+''"
                        v-for="item in menuList"
                        :key="item.id"
                    >
                        <template slot="title">
                            <i :class="iconsObj[item.id]"></i>
                            <span>{{item.authName}}</span>
                        </template>
                        <!-- 二级菜单 -->
                        <el-menu-item
                            :index="'/'+subItem.path"
                            v-for="subItem in item.children"
                            :key="subItem.id"
                            @click="saveNavState('/'+subItem.path)"
                        >
                            <i class="el-icon-menu"></i>
                            {{subItem.authName}}
                        </el-menu-item>
                    </el-submenu>

                </el-menu>
            </el-aside>
            <!-- 右边主体内容 -->
            <el-main>
                <router-view></router-view>
            </el-main>
        </el-container>

    </el-container>
</template>
<script>
export default {
    data() {
        return {
            // 左侧菜单列表
            menuList: [],
            // 图标
            iconsObj: {
                '125': 'iconfont icon-user',
                '103': 'iconfont icon-tijikongjian',
                '101': 'iconfont icon-shangpin',
                '102': 'iconfont icon-danju',
                '145': 'iconfont icon-baobiao',
            },
            // 是否折叠左侧菜单
            isCollapse: false,
            // 当前激活菜单的路径
            actionPath: ""

        }
    },
    created() {
        this.getMenuList();
        this.actionPath = sessionStorage.getItem('actionPath');
    },
    methods: {
        // 获取左侧菜单列表
        async getMenuList() {
            const { data: res } = await this.$http.get('menus');
            if (res.meta.status !== 200) {
                return this.$message.error(res.meta.msg);
            }
            this.menuList = res.data;
        },
        // 退出
        logout() {
            sessionStorage.clear();
            this.$router.push('/login');
        },
        // 保存激活菜单的状态
        saveNavState(actionPath) {
            sessionStorage.setItem('actionPath', actionPath);
            this.actionPath = actionPath;
        }
    },
}
</script>

<style lang="less" scoped>
.el-container {
    height: 100%;
}
.el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    // background-image: linear-gradient(90deg, #79a0cc, #6796e9);
    // background-image: linear-gradient(90deg, #a8dbf3, #8fd3f4);
    background-image: linear-gradient(90deg, #84fab0, #8fd3f4);
    > div {
        display: flex;
        align-items: center;
        img {
            width: 40px;
            margin-left: 10px;
        }
        span {
            color: #fff;
            font-size: 20px;
            margin-left: 20px;
        }
    }
    .avatar {
        width: 40px;
        height: 40px;

        img {
            width: 100%;
            height: 100%;
            border-radius: 50%;
        }
    }
    .el-dropdown-link {
        font-size: 14px;
    }
}
.el-aside {
    .el-menu {
        border-right: 0;
        .iconfont {
            margin-right: 10px;
        }
    }

    .toggle-buttom {
        background: #13e2a3;
        font-size: 10px;
        line-height: 24px;
        color: #fff;
        text-align: center;
        letter-spacing: 0.2rem;
        cursor: pointer;
    }
}
.el-main {
    width: 100%;
    height: 100%;
    background: #eaedf1;
}
</style>